package com.example.myapplication.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.MainActivity;
import com.example.myapplication.R;
import com.example.myapplication.domain.ClassAndTagItem;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    LayoutInflater mInflater; //声明布局填充器
    private List<ClassAndTagItem.DataDTO.ClassDTO> classXs;


    Context context; //声明上下文


    //构造方法
    public MyAdapter(Context context, List<ClassAndTagItem.DataDTO.ClassDTO> classXs) {
        mInflater = LayoutInflater.from(context); //获取布局服务
        this.classXs=classXs;
        this.context=context;
    }




    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate( R.layout.class_list_item, parent, false);
        //View view = LayoutInflater.from(parent.getContext()).inflate( R.layout.list_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        ClassAndTagItem.DataDTO.ClassDTO classX = classXs.get(position);
        holder.txt.setText(classX.getName());
        holder.txt.setGravity(Gravity.CENTER);
        MainActivity.setDrawableLeft(context, classX.getIcon(), holder.txt);

        //添加点击事件监听
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, classX.getName(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return classXs.size();
    }

    //自定义内部类 ViewHolder
    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView txt;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            txt=itemView.findViewById(R.id.txtClass);
        }

    }

}
